package zcjq.d912;

import java.util.concurrent.Semaphore;

public class P8 {

    Semaphore semaphore1 = new Semaphore(1);
    Semaphore semaphore2 = new Semaphore(0);
    Semaphore semaphore3 = new Semaphore(0);

    public void first(Runnable runnable) {
        try {
            semaphore1.acquire();
            runnable.run();
            semaphore2.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public void second(Runnable runnable) {
        try {
            semaphore2.acquire();
            runnable.run();
            semaphore3.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void third(Runnable runnable) {
        try {
            semaphore3.acquire();
            runnable.run();
            semaphore1.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new T2().start();
    }
}
class T2 extends Thread{
    T2(){
        setName("aa");
    }

    @Override
    public void run() {
        super.run();
    }
}
